<?php
// +----------------------------------------------------------------------
// | my
// +----------------------------------------------------------------------
// | Copyright (c) 2016~2022 http://baiyf.cn All rights reserved.
// +----------------------------------------------------------------------
// | Licensed ( http://www.apache.org/licenses/LICENSE-2.0 )
// +----------------------------------------------------------------------
// | Author: $this <498944516@qq.com>
// +----------------------------------------------------------------------
namespace app\printer\model;

use think\Model;

class WeRob extends Model
{

    protected $status=['待打印','打印机中','打印成功'];
    /**
     * 获取机器人
     */
    public function index($data=''){
      
       
       switch ($data['keyword']) {
          case 'image':
            $reply= $this->addPic($data);
            break;
          case 'P':
            $reply= $this->p($data);
            break;
          
          default:
            # code...
            break;
        } 



       return $reply;
      
    }

    // 微信打印机
   public function addPic($data) {
       
       
       //检测打印服务是否正常
        $printer=model('printer/Printer')->checkPrinter($data['appid'],$data['openid']);
        if(!$printer)  return ['message'=>'没有识别到打印机，请扫描二维码后打印！'];

        $printer['qrcode']=get_cover($printer['qrcode'], 'path');

        //检查打印机是否在线
        if (!cache('printer_online_'.$printer['shopid'])) {
         
          //  $reply=[
          //   'template_id'=>'LsIc21raK3kWuX8j8hgBwJ-1cWn35PIdxgz2KMxgMPQ',
          //   'url'=>'http://baidu.com',
          //   'data'=>[
          //   'first'=>['value'=>'打印机监控未开启'],
          //   'keyword1'=>['value'=>'打印机监控未开启'],
          //   'remark'=>['value'=>'打印机监控未开启']
          //   ]
          //   ];
          // templateMessage($pic['openid'],$reply);
          $admins=model('we/WeFans')->getAdmins($data['appid']);
          foreach ($admins as $key => $admin) {
           $reply=['msgtype'=>'text','text'=>['content'=>$admin['nickname'].',你好，/:fade我检测到微信打印机监控未开启，顾客正在尝试打印，请及时检查']];
           send($admin['openid'],$reply,'customMessage');
          }
          
          return ['message'=>$printer['title']."监控没有开启，请联系你周围的客服人员"];
        } 

         //检查用户是否拥有资格,并且扣除本次消费
        $balance=model('printer/PrinterFans')->checkFans($data['openid'],$printer);
      
        if($balance<0) {
          return ['message'=>'余额不足，请充值后打印！'];
        }
    
        //检测照片是否合格,处理照片
        $info=model('printer/PrinterPic')->checkPic($data,$printer);
 
        // 计数
        model('printer/Printer')->addTotal($printer['id']);
       

        if ($info['width']<720 || $info['height']<720) $warn=',照片质量偏低，请下次打印高质量图片';

        
        $message="/:hug".$printer['title']."已经接收任务!\n";
        
        //查询排队情况
        $message.="/:hug前面有".model('printer/PrinterPic')->printtingTotal($printer['id'])."张照片正在在排队打印，耐心等待哦!\n";

        $message.="/:rose照片尺寸".$info['width'].'X'.$info['height'].(isset($warn)?$warn:'')."！\n";
        $message.="/:rose剩余打印次数".$balance."\n";
        $message.="/:rose粉丝号".$data['fans']['id']."\n";
        return ['message'=>$message];
    }

    public function p($data) {
       //检查合法性
      
       if ($data['userid']==''){
        return ['message'=>'您还没有登录内部员工系统，回复：手机号@密码 登录'];
       }
       
       switch ($data['before']) {
         case '':
           if ($data['back']=='') return ['message'=> $this->order($data)];
           break;
         case '列表':
           $user=model('admin/User')->info($data['userid'],'shopid');
           mylog( $user);
           $cur_date = strtotime(date('Y-m-d',time()));
           $map['shopid']=array('eq',$user['shopid']);
           $map['create_time']=array('EGT',$cur_date);
           $pics=db('PrinterPic')->where($map)->limit(10)->select();
           $message="打印列表（最后10条）";
           foreach ($pics as $key => $pic) {
              $message.="\n".$pic['id'].'：'.$pic['nickname'].'-'.$this->status[$pic['status']];
           }
           return ['message'=>$message];

           break;

         case '再打':
           $res=db('PrinterPic')->where('id',$data['back'])->update(['status'=>0]);
           if ($res) return ['message'=>$data['back'].'号照片再次进入打印机'];
           return ['message'=>'再打失败'];

           break;
         
         default:
           $fans = model('we/WeFans')->infoForFans($data['before']);
           if (!$fans) return ['message'=>'粉丝不存在，请核对粉丝ID'];
           if ($data['back']==''){
            return ['message'=>$fans['message']]; //空尾巴查询
           }else{
            if (!is_numeric($data['back']))return ['message'=>'充值数量输入不合法'];
             $res=model('printer/PrinterFans')->pay($fans['fans']['openid'],$data['back']);
             return ['message'=>'您为'.$fans['fans']['nickname'].'充值照片'.$data['back'].'张'];
           }

           break;
       }
  

    }

    public function order($data) {
         $printers=db('printer')->where('aid',$data['aid'])->select();
          
         $message='打印机诊断工具';
         $online='';
        
         foreach ($printers as $key => &$printer) {
            if (cache('printer_online_'.$printer['shopid'])==1){
             $online='-在线/:rose';
            }else{
             $online="-离线/:fade \n请在打印机电脑打开".$_SERVER['HTTP_HOST']."/printer/?shopid=".$printer['shopid']; 
            }
            $message.="\n".$printer['title'].$online;
         }

         $message.="\n-------------";
         $message.="\n神奇的大写字母P：\n（P:我是您的打印助手，打印机Printer的首字母）";
         $message.="\n/:hug1.打印充值：\n12P1 (给12号粉丝充值1张)";
         $message.="\n/:hug2.查询粉丝信息：\n12P (查询12号粉丝信息)";
         $message.="\n/:hug3.打印列表：\n列表P";
         return $message;
     }

    
}